package com.iwinding.ladygg.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.iwinding.ladygg.R;
import com.iwinding.ladygg.constant.SPConstant;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.PersonInfoCallback;
import com.iwinding.ladygg.net.response.PersonInfoRp;
import com.iwinding.ladygg.ui.act.device.DeviceActivity;
import com.iwinding.ladygg.ui.act.InfoActivity;
import com.iwinding.ladygg.ui.act.SettingActivity;
import com.iwinding.ladygg.ui.act.SosNoticeActivity;
import com.iwinding.ladygg.ui.act.SosRecordActivity;
import com.iwinding.ladygg.utils.CommonUtil;
import com.iwinding.ladygg.utils.GsonUtil;
import com.iwinding.ladygg.utils.RoundedTransformation;
import com.iwinding.ladygg.utils.SPUtils;
import com.iwinding.ladygg.utils.ToastUtil;
import com.squareup.picasso.Picasso;

/**
 * 我的模块
 * Created by hezhiyong on 2017/3/13.
 */

public class MyselfFragment extends TabFragment implements View.OnClickListener {

    public static final int INFO_PAGE = 100;

    //基础信息
    private ImageView mImg_icon;
    private TextView mTxt_name;
    private TextView mTxt_id;

    //功能模块
    private View mLay_info;
    private View mLay_connet;
    private View mLay_sos_notice;
    private View mLay_record;
    private View mLay_setting;

    @Override
    public void initView() {
        matchView();
        setLinster();
        requestMyInfo();
    }

    @Override
    protected View onCreateRootView() {
        return View.inflate(getContext(), R.layout.icane_fragment_my, null);
    }


    /**
     * 匹配获取view
     */
    void matchView() {
        mImg_icon = (ImageView) findViewById(R.id.icane_my_img);
        mTxt_name = (TextView) findViewById(R.id.icane_my_txt_name);
        mTxt_id = (TextView) findViewById(R.id.icane_my_txt_id);

        mLay_info = findViewById(R.id.lay_my_info);
        mLay_connet = findViewById(R.id.lay_my_device_notice);
        mLay_sos_notice = findViewById(R.id.lay_my_sos_notice);
        mLay_record = findViewById(R.id.lay_my_device_record);
        mLay_setting = findViewById(R.id.lay_my_setting);
    }

    /**
     * 设置监听
     */
    void setLinster() {
        mLay_info.setOnClickListener(this);
        mLay_connet.setOnClickListener(this);
        mLay_sos_notice.setOnClickListener(this);
        mLay_record.setOnClickListener(this);
        mLay_setting.setOnClickListener(this);
    }

    /**
     * 获取基础信息
     */
    void requestMyInfo() {
        RequestManager.postPersonInfo(mContext, new PersonInfoCallback() {
            @Override
            public void onResult(PersonInfoRp personInfoRp) {
                SPUtils.putSpProperty(mContext, SPConstant.PERSONINFO, GsonUtil.getInstance().toJson(personInfoRp));
                String name = TextUtils.isEmpty(personInfoRp.nickName) ? (TextUtils.isEmpty(personInfoRp.name) ? personInfoRp.uname : personInfoRp.name) : personInfoRp.nickName;
                setInfo(personInfoRp.headImg, name, personInfoRp.userId);
            }

            @Override
            public void onFail(String errorMsg) {
                ToastUtil.openBaseToast(mContext, errorMsg, Toast.LENGTH_SHORT);
            }
        });
    }

    void setInfo(String url, String name, String id) {
        if (!TextUtils.isEmpty(url)) {
            url = CommonUtil.utf8Togb2312(url);
            Picasso.with(getContext()).load(url).resize(100,100)
                    .placeholder(R.mipmap.pub_default_avatar)
                    .error(R.mipmap.pub_default_avatar)
                    .transform(new RoundedTransformation(9, 0)).into(mImg_icon);
        }
        mTxt_name.setText(TextUtils.isEmpty(name) ? "" : name);
        mTxt_id.setText(getString(R.string.icane_my_id, TextUtils.isEmpty(id) ? "" : id));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_my_info:
                startActivityForResult(new Intent(mContext, InfoActivity.class), INFO_PAGE);
                break;
            case R.id.lay_my_device_notice:
                startActivity(new Intent(mContext, DeviceActivity.class));
                break;
            case R.id.lay_my_sos_notice:
                startActivity(new Intent(mContext, SosNoticeActivity.class));
                break;
            case R.id.lay_my_device_record:
                startActivity(new Intent(mContext, SosRecordActivity.class));
                break;
            case R.id.lay_my_setting:
                startActivity(new Intent(mContext, SettingActivity.class));
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INFO_PAGE && resultCode == Activity.RESULT_OK) {
            String str = SPUtils.getSpPropertyString(mContext, SPConstant.PERSONINFO, "");
            if (!TextUtils.isEmpty(str)) {
                PersonInfoRp personInfoRp = GsonUtil.getInstance().formJson(str, PersonInfoRp.class);
                setInfo(personInfoRp.headImg, personInfoRp.name, personInfoRp.userId);
            }
        }
    }
}
